package com.sp.api.springbootapi.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.sql.SQLException;

/**
 * 配置数据源
 * 
 * @author jerry
 *
 */
@Configuration
@PropertySource(value = "classpath:application.properties")
public class DataSourceConfig {
	
	@Value("${spring.datasource.druid.driver-class-name}")
	private String driverClassName;

	@Value("${spring.datasource.druid.url}")
	private String url;

	@Value("${spring.datasource.druid.username}")
	private String userName;

	@Value("${spring.datasource.druid.password}")
	private String password;

	@Bean
	public DruidDataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(driverClassName);
		dataSource.setUrl(url);
		dataSource.setUsername(userName);
		dataSource.setPassword(password);
//		dataSource.setMaxActive(arg0);
//		dataSource.setMaxWait(arg0);
		try {
            dataSource.init();
        } catch (SQLException e) {
            throw new RuntimeException("druid datasource init fail");
        }
		return dataSource;
	}
}